package com.jiuyin.dianjing.oss;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.GetObjectRequest;
import com.alibaba.sdk.android.oss.model.GetObjectResult;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.jiuyin.dianjing.model.FileModel;
import com.jiuyin.dianjing.util.LogUtil;

import java.io.File;
import java.util.HashMap;
import java.util.List;

/**
 * Created by oss on 2015/12/7 0007.
 * 支持普通上传，普通下载和断点上传
 */
public class OssService {

    //根据实际需求改变分片大小
    private final static int partSize = 256 * 1024;
    private OSS oss;
    /**
     * 这个名字和FOLDER_NAME无关
     */
    private String bucket = "jiuyindianjing";
    private String callbackAddress;


    public OssService(OSS oss) {
        this.oss = oss;
    }

    public void setCallbackAddress(String callbackAddress) {
        this.callbackAddress = callbackAddress;
    }

    /**
     * 下载
     *
     * @param object       文件名
     * @param userCallback 可以控制进度
     */
    public void asyncGetImage(String object,
                              @NonNull final OSSCompletedCallback<GetObjectRequest, GetObjectResult> userCallback) {
        if (TextUtils.isEmpty(object)) {
            LogUtil.log("asyncGetImage Object is Null");
            return;
        }

        GetObjectRequest get = new GetObjectRequest(bucket, object);
        OSSAsyncTask task = oss.asyncGetObject(get, userCallback);
    }

    /**
     * 上传
     *
     * @param object
     * @param localFile            文件地址
     * @param userCallback
     * @param userProgressCallback
     */
    public void asyncPutImage(String object,
                              String localFile,
                              @NonNull final OSSCompletedCallback<PutObjectRequest, PutObjectResult> userCallback,
                              final OSSProgressCallback<PutObjectRequest> userProgressCallback) {
        if (TextUtils.isEmpty(object)) {
            LogUtil.log("asyncPutImage Object is Null");
            return;
        }

        File file = new File(localFile);
        if (!file.exists()) {
            LogUtil.log("asyncPutImage File Not Exist");
            return;
        }

        // 构造上传请求
        PutObjectRequest put = new PutObjectRequest(bucket, object, localFile);
        if (callbackAddress != null) {
            // 传入对应的上传回调参数，这里默认使用OSS提供的公共测试回调服务器地址
            put.setCallbackParam(new HashMap<String, String>() {
                {
                    put("callbackUrl", callbackAddress);
                    //callbackBody可以自定义传入的信息
                    put("callbackBody", "filename=${object}");
                }
            });
        }

        // 异步上传时可以设置进度回调
        if (userProgressCallback != null) {
            put.setProgressCallback(userProgressCallback);
        }
        /*
        put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
            @Override
            public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
                //Log.d("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
                int progress = (int) (100 * currentSize / totalSize);

                ImageDisplayer.updateProgress(progress);
                ImageDisplayer.displayInfo("上传进度: " + String.valueOf(progress) + "%");
            }
        });*/
        oss.asyncPutObject(put, userCallback);
    }

    /**
     * @param localFiles           Pair<fileName,filePath>
     * @param userCallback
     * @param userProgressCallback
     */
    public void asyncPutImages(List<FileModel> localFiles, @NonNull final OSSCompletedCallback<PutObjectRequest, PutObjectResult> userCallback,
                               final OSSProgressCallback<PutObjectRequest> userProgressCallback) {
        for (int i = 0; i < localFiles.size(); i++) {
            String fileName = localFiles.get(i).fileName;
            String filePath = localFiles.get(i).filePath;
            asyncPutImage(fileName, filePath, userCallback, userProgressCallback);
        }
    }
}
